<?php

namespace app\common\model\game;

use app\common\enum\YesNoEnum;
use app\common\model\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 游戏管理模型
 * Class Game
 * @package app\common\model\game;
 */
class Game extends BaseModel
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';


    /**
     * @notes 设置图片域名
     * @param $value
     * @param $data
     * @return array|string|string[]|null
     * @author 段誉
     * @date 2022/9/28 10:17
     */
    public function getContentAttr($value, $data)
    {
        return get_file_domain($value);
    }


    /**
     * @notes 清除图片域名
     * @param $value
     * @param $data
     * @return array|string|string[]
     * @author 段誉
     * @date 2022/9/28 10:17
     */
    public function setContentAttr($value, $data)
    {
        return clear_file_domain($value);
    }

    /**
     * @notes 是否显示描述
     * @param $value
     * @param $data
     * @return string
     * @date 2024/7/17 19:18
     */
    public function getShowDescAttr($value, $data)
    {
        return $data['is_show'] ? '显示' : '不显示';
    }

    /**
     * @notes 是否完结描述
     * @param $value
     * @param $data
     * @return string
     * @date 2024/7/17 19:18
     */
    public function getGameVersionDescAttr($value, $data)
    {
        switch ($data['game_version']){
            case 1:
                $game_version_desc = '学习版';
                break;
            case 2:
                $game_version_desc = '入库版';
                break;
            case 3:
                $game_version_desc = '买正版';
                break;
            default:
                $game_version_desc = '';
        }
        return $game_version_desc;
    }

    /**
     * @notes 是否置顶推荐
     * @param $value
     * @param $data
     * @return string
     * @date 2024/7/17 19:18
     */
    public function getRecommendDescAttr($value, $data)
    {
        return $data['is_recommend'] ? '已置顶' : '未置顶';
    }


    /**
     * @notes 获取游戏详情
     * @param $id
     * @return array
     * @date 2024/7/15 0:39
     */
    public static function getGameDetailArr(int $id)
    {
        $info = Game::where(['id' => $id, 'is_show' => YesNoEnum::YES])
            ->field('id, name, eng_name, short_desc, image, mini_image, game_version, magnet_url, sale_url, minimum_config, sort, intro, download_num, is_show, is_recommend, create_time, update_time')
            ->findOrEmpty();

        if ($info->isEmpty()) {
            return [];
        }

        return $info->toArray();
    }

    /**
     * @notes 获取游戏列表
     * @param $ids_arr
     * @return array
     * @date 2024/7/15 0:39
     */
    public static function getGameByIds(array $ids_arr)
    {
        $where = [
            ['id', 'IN', $ids_arr],
            ['is_show', '=', YesNoEnum::YES]
        ];
        $list = Game::where($where)
            ->field('id, name, eng_name')
            ->order('id asc')
            ->select()
            ->toArray();

        return $list;
    }

}